package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 新增菜品和对应的口味
     */
    @Transactional
    public void saveWithFlavors(DishDTO dishDTO) {
        //创建一个Dish对象储存菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //调用增加菜品的mapper并获得生成的Id
        dishMapper.insert(dish);
        //创建一个DishFlavor对象集合储存口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() != 0) {
            for (DishFlavor flavor : flavors) {
                //往flavor中注入id
                flavor.setDishId(dish.getId());
            }
            //调用储存口味的mapper批量存储口味
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dto
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dto) {
        //调用PageHelper中的分页查询方法
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //调用mapper中的方法返回一个page对象
        Page<DishVO> dishVOPage = dishMapper.pageQuery(dto);
        //创建一个PageResult对象储存total和result
        PageResult pageResult = new PageResult(dishVOPage.getTotal(), dishVOPage.getResult());

        //返回PageResult对象
        return pageResult;
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //查数据库看看是否是起售状态，起售状态不能删，要抛出异常DeletionNotAllowedException
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == (StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //查数据库看看是否关联其他套餐，关联了不能删，要抛出异常DeletionNotAllowedException
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        if (setmealIds != null && setmealIds.size() != 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //批量删除菜品
        dishMapper.deleteByIds(ids);

        //批量删除口味
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品数据
     *
     * @param id
     * @return
     */
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.getById(id);
        BeanUtils.copyProperties(dish, dishVO);
        //调用CategoryMapper中的getById方法获得一个Category对象并取出其中的name
        /*Category category = categoryMapper.getById(dish.getCategoryId());
        dishVO.setCategoryName(category.getName());*/
        //获得口味数据
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(dish.getId());
        dishVO.setFlavors(flavors);
        //返回一个DishVO对象
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();

        dishMapper.update(dish);
        //修改口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());
        if (flavors != null && flavors.size() != 0) {
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .status(StatusConstant.ENABLE)
                .categoryId(categoryId)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 菜品起售或停售
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }
}
